import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.util.Calendar;
public class step4_2 {
    int a = -1;         //创建一个全局变量
    JFrame jf = new JFrame("step4_2");
    static JTextArea jta1, jta2;        //创建两个文本框
    Thread trda = new thread1();        //创建两个进程
    Thread trdb = new thread2();        //创建两个进程

    public String showtime() // 获取当前时间并且判断相关条件
    {
        int y, m, d, h, mi, s;
        Calendar cal = Calendar.getInstance(); // 获取一个Calendar类的实例对象
        y = cal.get(Calendar.YEAR); // 获取年份
        m = cal.get(Calendar.MONTH); // 获取月份,获取的月份是从0到11表示一到十二月
        d = cal.get(Calendar.DATE); // 获取日期
        h = cal.get(Calendar.HOUR_OF_DAY); // 获取小时
        mi = cal.get(Calendar.MINUTE); // 获取分钟
        s = cal.get(Calendar.SECOND); // 获取秒钟
        if (h == 8)
            a = 0;
        else if (h == 23 || h > 0 & h < 8)
            a = 1;
        else
            a = -1;

        return "现在时刻是" + y + "年" + (m + 1) + "月" + d + "日" + h
                + "时" + mi + "分" + s + "秒";
    }

    //线程trda
//线程trdb
    public step4_2()
    //构造函数,生成图形界面
    {
//setBounds(100,100,500,200);
        jf.setLayout(new FlowLayout());
        jf.setSize(700, 300);
        jta1 = new JTextArea(15, 30);
        jta2 = new JTextArea(15, 30);
        jta1.setEditable(false);
        jta2.setEditable(false);
        jf.add(jta1);
        jf.add(jta2); //将2个组件添加到界面上
        jf.setLocation(100, 150);
        jf.setVisible(true);
        jf.pack();
        trda.start();
        trdb.start();
        jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
    }

    //两个线程都启动
    public static void main(String[] args) {
        step4_2 frm = new step4_2();
    }

    class thread1 extends Thread
            //线程类thread1
    {
        public void run() {
            while (true) {
                step4_2.jta1.setText(showtime());
                try {
                    thread1.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } }
    }}

    class thread2 extends Thread
            //线程类thread2
    {
        public void run() {
            while (true) {
                if (a == 1)
                    step4_2.jta2.setText("你累了");
                else if (a == 0)
                    step4_2.jta2.setText("该上课了");
                else
                    step4_2.jta2.setText(null);
                try {
                    thread2.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
